<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view>
        <view class="name titleBold">{{ info.name }}</view>
        <view v-if="info.tips" class="tips"> {{ info.tips }} </view>
        <view class="sickList">
          <view
            v-for="item in info.options"
            :key="item.id"
            :class="[info.rowBlock === true ? 'sickBox blocks' : 'sickBox']"
          >
            <view :class="[checkValue === item.id ? 'sickSpan cur' : 'sickSpan']" @click="choseList(item)">
              {{ item.name }}
              <span v-if="checkValue === item.id" class="curImg"><img :src="curIcon" /></span>
            </view>
            <view v-if="item.tips" class="kidTips">{{ item.tips }}</view>
          </view>
        </view>
        <view v-if="checkValue === 30">
          <view class="inputBox">
            <span class="name lineHeight86">戒烟</span>
            <input
              id="quitSmokingYear"
              v-model="mainData.quitSmokingYear"
              type="digit"
              class="uni-input lineHeight86"
              name="height"
              placeholder-class="phcolor"
              @change="inputChange($event)"
            />
            <view class="unit">年</view>
            <view class="clear"></view>
          </view>
          <view class="inputBox">
            <span class="name lineHeight86">之前抽烟</span>
            <input
              id="preSmokingYear"
              v-model="mainData.preSmokingYear"
              type="digit"
              class="uni-input lineHeight86"
              name="preSmokingYear"
              placeholder-class="phcolor"
              @change="inputChange($event)"
            />
            <view class="unit">年</view>
            <view class="clear"></view>
          </view>
          <view class="inputBox">
            <span class="name lineHeight86">每天</span>
            <input
              id="height"
              v-model="mainData.preSmokingBags"
              type="digit"
              class="uni-input lineHeight86"
              name="height"
              placeholder-class="phcolor"
              @change="inputChange($event)"
            />
            <view class="unit">包</view>
            <view class="clear"></view>
          </view>
        </view>
        <view v-if="checkValue === 40">
          <view class="inputBox">
            <span class="name lineHeight86">抽烟</span>
            <input
              id="smokingYear"
              v-model="mainData.smokingYear"
              type="digit"
              class="uni-input lineHeight86"
              name="height"
              placeholder-class="phcolor"
              @change="inputChange($event)"
            />
            <view class="unit">年</view>
            <view class="clear"></view>
          </view>
          <view class="inputBox">
            <span class="name lineHeight86">每天</span>
            <input
              id="smokingBags"
              v-model="mainData.smokingBags"
              type="digit"
              class="uni-input lineHeight86"
              name="height"
              placeholder-class="phcolor"
              @change="inputChange($event)"
            />
            <view class="unit">包</view>
            <view class="clear"></view>
          </view>
        </view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import { checkBetweenDecimalOne } from '@/common/assessKeyCheck';
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    sickTitleCom
  },
  props: {
    // 是否展示搜索按钮
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      checkValue: undefined,
      curIcon: curIcon,
      noChose: false,
      mainData: {
        quitSmokingYear: '',
        preSmokingYear: '',
        preSmokingBags: '',
        smokingYear: '',
        smokingBags: ''
      }
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    }
  },
  mounted() {},
  methods: {
    // eslint-disable-next-line complexity
    async nextBtn() {
      // 疾病直接提交接口
      let submitData = this.assessForm;
      submitData[this.info.key] = this.checkValue;
      if (!this.checkValue && this.checkValue !== 0) {
        uni.showToast({ title: `请选择${this.info.name}`, icon: 'none' });
        return false;
      }
      if (this.checkValue === 30) {
        this.mainData.smokingYear = '';
        this.mainData.smokingBags = '';
      } else if (this.checkValue === 40) {
        this.mainData.quitSmokingYear = '';
        this.mainData.preSmokingYear = '';
        this.mainData.preSmokingBags = '';
      } else {
        this.mainData.smokingYear = '';
        this.mainData.smokingBags = '';
        this.mainData.quitSmokingYear = '';
        this.mainData.preSmokingYear = '';
        this.mainData.preSmokingBags = '';
      }
      if (this.checkValue === 30 && !this.mainData.quitSmokingYear) {
        uni.showToast({ title: `请输入戒烟年数`, icon: 'none' });
        return false;
      }
      if (this.checkValue === 30 && !this.mainData.preSmokingYear) {
        uni.showToast({ title: `请输入之前抽烟年数`, icon: 'none' });
        return false;
      }
      if (this.checkValue === 30 && !this.mainData.preSmokingBags) {
        uni.showToast({ title: `请输入每日吸烟量`, icon: 'none' });
        return false;
      }
      if (this.checkValue === 40 && !this.mainData.smokingYear) {
        uni.showToast({ title: `请输入抽烟年数`, icon: 'none' });
        return false;
      }
      if (this.checkValue === 40 && !this.mainData.smokingBags) {
        uni.showToast({ title: `请输入每日吸烟量`, icon: 'none' });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.quitSmokingYear, '戒烟几年', 1, 100)) {
        uni.showToast({ title: `戒烟几年数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.preSmokingYear, '已经抽烟几年', 1, 100)) {
        uni.showToast({
          title: `之前抽烟几年数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.preSmokingBags, '每天抽烟几包', 0.1, 10)) {
        uni.showToast({ title: `每日吸烟量错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.smokingYear, '抽烟几年', 1, 100)) {
        uni.showToast({ title: `抽烟几年数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetweenDecimalOne(this.mainData.smokingBags, '每天抽几包', 0.1, 10)) {
        uni.showToast({ title: `每日吸烟量错误，请重新输入`, icon: 'none' });
        return false;
      }
      Object.keys(this.mainData).forEach((key) => {
        submitData[key] = this.mainData[key];
      });
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      const healthData = await ajaxSend(Object.assign(this.mainData, { smok: this.checkValue }));
      let curActive = this.active + 1;
      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    choseList(val) {
      this.checkValue = val.id;
    },
    inputChange(e) {}
  }
};
</script>
<style lang="scss">
@import './../style/radioInput.scss';
</style>
